#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/5/18 0:17
# @Author  : Grayson Liu
# @Email   : graysonliu@foxmail.com
# @File    : power_ssj2008.py

import re
from typing import Union
import pandas as pd


def company_rule(x: str) -> str:
    """
    对Company这一列数据的处理操作，用于DataFrame的map方法
    :param x: Hardware Vendor属性的值
    :return: 处理后的值
    """
    # 去掉公司后缀
    x = x.replace("Inc", "")
    x = x.replace("CO., LTD.", "")
    x = x.replace("Co., Ltd.", "")
    x = x.replace(".", "")
    x = x.replace(",", "")
    # 统一命名
    x = x.replace("Incoporated", "Incorporated")
    x = x.replace("Corp.", "Corporation")
    x = x.replace("Supermicro", "Super Micro Computer")
    x = x.replace("Hewlett-Packard", "Hewlett Packard")

    return x


def system_rule(x: str) -> str:
    y = re.sub(r"\(.*\)", "", x)  # 删除括号中的内容
    return y


def processor_rule(x: str) -> str:
    y = re.sub(r"(CPU|[2|3][.]|@|,).*G[H|h]z", "", x)
    y = re.sub(r"\(.*\)", "", y)
    y = y.replace("processor ", "")
    # y = y.replace("processor ", "")
    return y


def first_cache_rule(line: pd.Series) -> str:
    x = line["1st Level Cache"]
    cores_per_chip = line["# Cores Per Chip"]
    # cores_num = line["# cores"]
    if x.find("per core") != -1 & x.find("I+D") == -1:
        # 说明：(group1: 1个以上数字)+'KB'+可能有空格+'(I)'或'I'+可能有空格+'+'+可能有空格+(group2: 1个以上数字)+'KB'+可能有空格+'(D)'或'D'
        search_obj = re.match(r"(\d+)\s*KB\s*(?:\(I\)|I|)\s*\+\s*(\d+)\s*KB\s*(?:\(D\)|D|)", x)
        l1i_kb = search_obj.group(1)
        l1d_kb = search_obj.group(2)
        return f"{l1i_kb}KB(I)+{l1d_kb}KB(D)"
    elif x.find("I+D") != -1:
        # 说明：(group1: 1个以上数字)+可能有空格+'KB'
        search_obj = re.match(r"(\d+)\s*KB", x)
        l1_kb = search_obj.group(1)
        return f"{l1_kb}KB(I+D)"
    elif x.find("per chip") != -1 & x.find("micro-ops") == -1:
        search_obj = re.match(r"(\d+)\s*KB\s*(?:\(I\)|I|)\s*\+\s*(\d+)\s*KB\s*(?:\(D\)|D|)", x)
        l1i_kb = int(int(search_obj.group(1)) / cores_per_chip)
        l1d_kb = int(int(search_obj.group(2)) / cores_per_chip)
        return f"{l1i_kb}KB(I)+{l1d_kb}KB(D)"
    elif x.find("micro-ops") != -1:
        search_obj = re.match(r"(\d+)\s*K\s*micro-ops\s*(?:\(I\)|I|)\s*\+\s*(\d+)\s*KB\s*(?:\(D\)|D|)", x)
        l1i_kb = int(int(search_obj.group(1)) / cores_per_chip)
        l1d_kb = int(int(search_obj.group(2)) / cores_per_chip)
        return f"{l1i_kb}KB (I)+{l1d_kb}KB(D)"


def second_cache_rule(line: pd.Series) -> str:
    """
    对2nd Cache这一列数据的处理操作，用于DataFrame的apply方法（需要用到其他属性的值）
    :param line: DataFrame的一行，类型是pd.Series
    :return: 处理后的2nd Cache per core(KB)值
    """
    x = line["2nd Level Cache"]
    cores_per_chip = line["# Cores Per Chip"]
    # cores_num = line["# cores"]
    if x.find("per core") != -1 or x.find("per dual core") != -1:
        if x.find("M") + x.find("MB") != -2:
            search_obj = re.match(r"(\d+(\.\d+)|\d+)(\s*|)(MB|M)", x)
            l2_kb = (int(search_obj.group(1)) * 1024)
        else:
            search_obj = re.match(r"(\d+(\.\d+)|\d+)(\s*|)KB", x)
            l2_kb = search_obj.group(1)
        return f"{l2_kb}KB"
    elif x.find("per chip") != -1 or x.find("/ chip") != -1 & x.find("shared") == -1:
        if x.find("M") + x.find("MB") != -2:
            search_obj = re.match(r"(\d+)\s*MB", x)
            l2_kb = int((int(search_obj.group(1)) * 1024) / cores_per_chip)
        else:
            search_obj = re.match(r"(\d+)\s*KB", x)
            l2_kb = int(int(search_obj.group(1)) / cores_per_chip)

        return f"{l2_kb}KB"
    elif x.find("shared") != -1:
        search_obj = re.match(r"(\d+)(\s*|)MB", x)
        l2_kb = int((int(search_obj.group(1)) * 1024) / 2)
        return f"{l2_kb}KB"


def third_cache_rule(line: pd.Series) -> str:
    x = line["3rd Level Cache"]
    cores_per_chip = line["# Cores Per Chip"]
    # cores_num = line["# cores"]
    if x.find("shared") != -1 or x.find("shard") != -1 or x.find("Shared") != -1:
        # search_obj = re.match(r"((\d+)|(\d)(\s*|)MB(\s*|)(s|S)har(e|)d(\s*|)\/(\s*|)(\d)", x)
        # print(search_obj.group())
        # print(search_obj.group(2))
        # l3_gb = int(int(search_obj.group(1))/int(search_obj.group(2)))
        pattern = re.compile(r'\d+')
        search_obj = pattern.findall(x)
        # print(search_obj)
        # print(search_obj[1])
        # print(search_obj[2])
        l3_gb = round(int(search_obj[1]) / int(search_obj[2]), 2)
        return f"{l3_gb}MB"

    elif x.find("per chip") != -1 & x.find("shared") == -1:
        if x.find("M") + x.find("MB") != -2:
            search_obj = re.match(r"(\d+)\s*MB", x)
            l3_gb = int(search_obj.group(1))
        else:
            search_obj = re.match(r"(\d+)\s*KB", x)
            l3_gb = int(int(search_obj.group(1)) / 1024)
        return f"{l3_gb}MB"
    elif x.find("per core") != -1:
        search_obj = re.match(r"(\d+)\s*MB", x)
        l3_gb = int(search_obj.group(1)) * cores_per_chip
        return f"{l3_gb}MB"
    else:
        return "0"


def memory_rule(x: str) -> str:
    if type(x) == str:
        y = x.replace(",", "")
        search_obj = re.match(r"\d+", y)
        resu = search_obj.group(0)
        return resu + "GB"
    else:
        y = x + "GB"
        return y


def report_link_rule(x: str) -> str:
    """
    对Report Link这一列数据的处理操作，用于DataFrame的map方法
    :param x: Report Link属性的值
    :return: 处理后的Report Link值
    """
    search_obj = re.match(r'(http:.+html)"<A', x)
    return search_obj.group(1)
